#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_timer.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_ops.h"
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include "nvs_storage_hal.h"
#include "i2c_custom.h"
#include "aht20_hal.h"
#include "pcf8563.h"
#include "led_hal.h"
#include "sys_running_time.h"
#include "hlw8032.h"
#include "ch423.h"
#include "mqtt_hal.h"
#include "esp_now_hal.h"
#include "wifi_hal.h"
#include "lvgl.h"
#include "demos/lv_demos.h"
#include "lv_port.h"
#include "mutex.h"

static const char *TAG = "main";

static void dump_haps(void *arg)
{
    while (1)
    {
        size_t size = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
        size_t size_internal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
        ESP_LOGW(TAG, "RAM FREE SIZE --> PSRAM size=%d, SRAM sizeinternal=%d", size, size_internal);
        vTaskDelay(pdMS_TO_TICKS(5000));
    }
}

void app_main(void)
{
    /* sys init start */
    nvs_init();
    i2c_init();
    esp_now_mqtt_wifi_init();
    vTaskDelay(pdMS_TO_TICKS(50));
    /* sys init end */

    /* freertos create task start */
    xTaskCreate(dump_haps, "dump_haps", 1024 * 4, NULL, 2, NULL);
    xTaskCreate(aht_task, "aht_task", 1024 * 4, NULL, 3, NULL);
    xTaskCreate(pcf8563_task, "pcf8563_task", 1024 * 4, NULL, 4, NULL);
    xTaskCreate(ch423_task, "ch423_task", 1024 * 4, NULL, 5, NULL);
    xTaskCreate(hlw8032_task, "hlw8032_task", 1024 * 4, NULL, 6, NULL);
    xTaskCreate(wifi_status_task, "wifi_status_task", 1024 * 4, NULL, 1, NULL);
    xTaskCreate(esp_now_task, "esp_now_task", 1024 * 4, NULL, 1, NULL);
    xTaskCreate(mqtt_task, "mqtt_task", 1024 * 8, NULL, 7, NULL);
    xTaskCreate(lv_gui_task, "gui", 1024 * 10, NULL, 9, NULL);
    /* freertos create task end */
    while (1)
    {
        vTaskDelay(pdMS_TO_TICKS(2000));
    }
}
